﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Decision.IService;
using FantastPlatform.Framework;
using FantastPlatform.Decision.IModel;
using FantastPlatform.Framework.Core;

namespace FantastPlatform.Decision.UI.Service
{
    public partial class EmployeeContactService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            IPoliceContactService EcService = ServiceFactory.CreateService<IPoliceContactService>();
            IPoliceContact Eclist = new FantastPlatform.Decision.Model.PoliceContact();
            ConditionGroup group = new ConditionGroup();
            Condition enableCondition = new Condition() { Field = "Enable", Type = typeof(bool).FullName, Value1 = true };
            group.Conditions.Add(enableCondition);
            switch (Action)
            {
                //case "Add":
                //    string data = Data;
                //    PoliceContactListView view = (PoliceContactListView)JsonToEntity<PoliceContactListView>(RepairString(data));
                //    if (view != null)
                //    {
                //        Eclist.Id = view.Id;
                //        Eclist.Name = view.Name;
                //        Eclist.Code = view.Code;
                //        Eclist.Value = view.Value;
                //    }
                //    if (Eclist.Id == 0)
                //    {
                //        EcService.Create(Eclist);
                //        ReturnSuccessMessage("保存成功");
                //    }
                //    else
                //    {
                //        IPoliceContact tellist1 = EcService.Get(view.Id);
                //        Eclist.Name = view.Name;
                //        Eclist.Code = view.Code;
                //        Eclist.Value = view.Value;

                //        tellist1.ModifyDate = System.DateTime.Now;
                //        EcService.Update(Eclist);
                //        ReturnSuccessMessage("更新成功");
                //    }
                //    break;
                case "Search":
                    int total;
                    object[] dl;
                    group.Conditions.Add(enableCondition);
                    IList<IPoliceContact> DtList = EcService.Search(group, null, PageSize, SelfPage, out total);
                    dl = (from m in DtList
                          select new
                          {
                              Id = m.Id,
                              Name = m.Name,
                              Code = m.Code,
                              Value = m.Value
                          }).OrderByDescending(m => m.Id).ToArray();
                    ReturnList(total, dl);
                    break;
                //case "Get":
                //    Eclist = EcService.Get(Id);
                //    object Dtobj = new
                //    {
                //        Id = Eclist.Id,
                //        Name = Eclist.Name,
                //        Code = Eclist.Code,
                //        Value = Eclist.Value
                //    };
                //    ReturnObj(Dtobj);
                //    break;
                //case "Delete":
                //    if (string.IsNullOrEmpty(Data))
                //    {
                //        throw new Exception("传入的ID为空");
                //    }
                //    List<int> idList = new List<int>();
                //    string[] idStrList = Data.Split(',');
                //    foreach (string id in idStrList)
                //    {
                //        idList.Add(int.Parse(id));
                //    }
                //    EcService.Delete(idList);
                //    ReturnSuccessMessage("删除成功");
                //    break;
                case "SearchByCondition":
                    PoliceContactListView view = (PoliceContactListView)JsonToEntity<PoliceContactListView>(RepairString(Data));
                    int searchTotal = 0;
                    if (!string.IsNullOrEmpty(view.Name))
                    {
                        Condition dltsearch = new Condition() { Field = "Name", Type = typeof(string).FullName, Value1 = "%" + view.Name + "%", Comparison = Comparisons.Like, Relation = Relations.And };
                        group.Conditions.Add(dltsearch);
                    }
                    if (!string.IsNullOrEmpty(view.Code))
                    {
                        Condition dltsearch = new Condition() { Field = "Code", Type = typeof(string).FullName, Value1 = "%" + view.Code + "%", Comparison = Comparisons.Like, Relation = Relations.And };
                        group.Conditions.Add(dltsearch);
                    }
                    DtList = EcService.Search(group, null, PageSize, SelfPage, out searchTotal);
                    dl = (from m in DtList
                          select new
                          {
                              Id = m.Id,
                              Name = m.Name,
                              Code = m.Code,
                              Value = m.Value
                          }).OrderByDescending(m => m.Id).ToArray();
                    ReturnList(searchTotal, dl);
                    break;

            }
        }
        public class PoliceContactListView
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Code { get; set; }
            public string Value { get; set; }
        }
    }
}